param(
  $InputFile = 'toc.md',
  $OutputFile = '目录.md',
  [Int32]$Deep = 5
)

$toc = Get-Content -Path $InputFile | Where-Object { $_ -ne '' }

# 第1章 从前端基础到Vue.js 3 1 -> 第1章 从前端基础到Vue.js 3
$toc = $toc -replace '\s(\d+)$', ''

# 循 环 渲 染 -> 循环渲染 
# (章 处)要保留
$toc = $toc -replace '((?!章)([一-龟])) ','$1'

# 第1篇
$toc = $toc -replace '^(第\d+篇)', '# $1'

# 第1章
$toc = $toc -replace '^(第\d+章)', '## $1'

# 1.1
$toc = $toc -replace '^(\d+\.\d+[^\.])', '### $1'

# 1.1.1
$toc = $toc -replace '^(\d+\.\d+\.\d+[^\.])', '#### $1'

$prefix = '#' * ($Deep + 1)
$headerNot = "^$prefix.*$"

$toc = $toc | Where-Object {-not ($_ -match $headerNot)}

$toc | Out-File $OutputFile -Encoding utf8
